<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

    require APPPATH . 'libraries/reports/mpdf/mpdf.php';
    require APPPATH . 'libraries/reports/mpdf/config.php';
    
    
class  Library_Mpdf extends mPDF{
    /* @var  config
     */
    private $config_default = array('mode'=>'','format'=>'A4',
                            'font_size'=>0,
                            'font_name'=>'',
                            'margin_left'=>15,
                            'margin_right'=>15,
                            'margin_top' =>16,
                           'margin_bottom'=>16,
                           'margin_heigh'=>9,
                           'margin_footer'=>9,
                           'orientation'  => 'P' // (P)portait (L) landscape
                           ); 
    private $data = array();
    private $_index = 0;
    private $config = array();
    function __construct($config = array()) {
         
        $this->config[$this->_index] = array_merge($this->config_default, $config);
        
        log_message('debug', 'Class Library_Mpdf initialized');
       // $this->mPDF($mode, $format, $default_font_size, $default_font, $mgl, $mgr, $mgt, $mgb, $mgh);
        
    }
//    function mPDF($mode = '', $format = 'A4', $default_font_size = 0, $default_font = '', $mgl = 15, $mgr = 15, $mgt = 16, $mgb = 16, $mgh = 9, $mgf = 9, $orientation = 'P') {
//        parent::mPDF($mode, $format, $default_font_size, $default_font, $mgl, $mgr, $mgt, $mgb, $mgh, $mgf, $orientation);
//    }
    function add_html($data){
         
        $this->data[$this->_index] = $data;
    }
    
    function add_page(){
        $this->_index +=1;
    }
     
    function add_config($config){
        $this->config[$this->_index] = array_merge($this->config_default, $config);
    }
    function display(){
        $config = $this->config['0'];
       $this->mPDF($config['mode'], 
                     $config['format'], 
                $config['font_size'], 
                $config['font_name'], 
                $config['margin_left'],
                $config['margin_right'], 
                $config['margin_top'],
                $config['margin_bottom'],
                $config['margin_height'],
                $config['margin_footer'], 
                $config['orientation']);
        
        
        foreach ($this->data as $key=>$html):
            $config = isset($this->config[$key]) ? $this->config[$key] : $this->config['0'];
            
            $this->SetMargins($config['margin_left'], $config['margin_right'], $config['margin_top']);
            $this->AddPage($config['orientation']);
            $this->WriteHTML($html);
        endforeach;
        $this->Output();
    }
    
    
}    
